<?php
namespace Opencart\Admin\Model\Extension\ExportImport\Module;

class ExportImport extends \Opencart\System\Engine\Model {

    private $codename = 'export_import';

    public function getLanguages(){
        $this->load->model('localisation/language');

        $languages = $this->model_localisation_language->getLanguages();
        $result = array();
        if (count($languages)>1) {
        $result[] = array(
            'language_id' => -1,
            'name' => $this->language->get('text_all_languages')
        );
        }
        if($languages){
            foreach ($languages as $language) {
                $result[] = array(
                    'language_id' => $language['language_id'],
                    'name' => $language['name']
                );
            }
        }
        return $result;
    }

    public function getModules(){
        $dir = DIR_EXTENSION. "export_import/system/config/export_import/*.php";
       
        $files = glob($dir);
        
        $result = [];
        foreach($files as $file){
            $fileName = basename($file, '.php');
            $setting = $this->getModuleSetting($fileName);
          
            if(empty($setting['opencart_version'])){
                $result[] = $fileName;
            }
            else{
                if(in_array(VERSION, $setting['opencart_version'])){                
                    $result[] = $fileName;
                }
            }
        }

        if (is_file(DIR_EXTENSION. "export_import_pro/install.json")) {
            $dir = DIR_EXTENSION. "export_import_pro/system/config/export_import/*.php";
            $files = glob($dir);
            foreach($files as $file){
                $fileName = basename($file, '.php');
                $setting = $this->getModuleSetting($fileName);

                $result[] = $fileName;
            }
        }
       
        return $result;
    }

    public function getModuleSetting($codename){
        $results = array();

        $file = DIR_EXTENSION. "export_import/system/config/export_import/".$codename.'.php';

        if (is_file($file)) {
            $_ = array();

            require($file);

            $results = array_merge($results, $_);
        }

        if (is_file(DIR_EXTENSION. "export_import_pro/install.json")) {
            $file = DIR_EXTENSION. "export_import_pro/system/config/export_import/".$codename.'.php';

            if (is_file($file)) {
                $_ = array();

                require($file);

                $results = array_merge($results, $_);
            }
        }
        
        return $results;
    }

    public function getModuleFilters($codename){
        $setting =$this->getModuleSetting($codename);
        $results = array();

        if(!empty($setting['main_sheet']['columns'])){
            $results = $setting['main_sheet']['columns'];
        }

        $results = array_filter($results, function($value){
            return !empty($value['filter'])?true:false;
        });

        $results = array_map(function($value) use ($setting){
            if(!empty($value['concat'])){
                $table = array_filter($setting['main_sheet']['tables'] ,function($filter_value) use ($value){
                    return $filter_value['name'] == $value['table'];
                });
                if(!empty($table)){
                    $value['table'] = array_pop($table);
                }
            }
            return $value;
        }, $results);

        return $results;
    }

    public function getRiotTags(){
        $result = array();
        $dir = DIR_EXTENSION. "export_import/admin/view/template/tags/*.tag";
        $files = glob($dir, GLOB_BRACE);
        foreach($files as $file){
            $result[] = HTTP_CATALOG . "extension/export_import/admin/view/template/tags/".basename($file).'?'.rand();
        }

        if (is_file(DIR_EXTENSION. "export_import_pro/install.json")) {
            $dir = DIR_EXTENSION. "export_import_pro/admin/view/template/tags/*.tag";
            $files = glob($dir, GLOB_BRACE);
            foreach($files as $file){
                $result[] = HTTP_CATALOG . "extension/export_import_pro/admin/view/template/tags/".basename($file).'?'.rand();
            }
        }
        
        return $result;
    }

    public function save($type) {
        $upload_tmp_dir = ini_get('upload_tmp_dir');
        if (!empty($upload_tmp_dir)) {
            $dir_tmp = $upload_tmp_dir;
        } elseif (!empty($this->request->sever['TMPDIR'])) {
            $dir_tmp = $this->request->sever['TMPDIR'];
        } else {
            $dir_tmp = DIR_CACHE.$this->codename;
        }

        $temp = tempnam($dir_tmp, 'zip');
        $zip = new \ZipArchive();
        $zip->open($temp, \ZipArchive::OVERWRITE);

        foreach (glob(DIR_CACHE.$this->codename."/*.xlsx") as $file) {
            $basename = basename($file);
            $zip->addFile($file, $basename);
        }

        $zip->close();

        header('Pragma: public');
        header('Expires: 0');
        header('Content-Description: File Transfer');
        header('Content-Type: mbooth/xml');
        header('Content-Disposition: attachment; filename=' . $type.'_'.date("Y-m-d_H-i-s")  . '.zip');
        header('Content-Transfer-Encoding: binary');

        readfile($temp);
        unlink($temp);

        $this->clearFolder(DIR_CACHE.$this->codename);
    }

   
}
